package primalgorithms.array;

import java.util.ArrayList;

public class _01DeleteDuplicates {
    public static int removeDuplicates(int[] nums) {
        /*
        //效率较低
        int len = nums.length;
        if(len == 0){
            return 0;
        }
        ArrayList<Integer> arrayList = new ArrayList<>();
        for (int i = 0;i < len;i++){
            //添加元素
            if(!arrayList.contains(nums[i])){
                arrayList.add(nums[i]);
            }
        }
        for (int i = 0;i < arrayList.size();i++){
            nums[i] = arrayList.get(i);
        }
        return arrayList.size();
        */

        //下面采用双指针法
        if(nums == null || nums.length == 0){
            return 0;
        }
        int left = 0;
        for (int right = 1;right < nums.length;right++){
            if(nums[left] != nums[right]){
                left++;
                nums[left] = nums[right];
            }
        }
        //注意left是下标，所以应该返回长度值为下标+1
        return ++left;
    }
}
